package com.atguigu.juc;

import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author ：MJS
 * @date ：Created in 2021/4/26  19:44
 * @description：阻塞队列
 *
 * 当队列是空的，从队列中获取元素的操作将会是阻塞
 * 当队列是满的，从队列中移除元素的操作将会被阻塞
 */
public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String>blockingQueue=new ArrayBlockingQueue<>(3);//数量需要自己设置
          //第一种情况：
          //插入
          blockingQueue.add("a");
          blockingQueue.add("b");
          blockingQueue.add("c");
//          blockingQueue.add("d");
          //检查
      System.out.println(blockingQueue.element());
        System.out.println(new Date());

        //移除
         System.out.println(blockingQueue.remove());
         System.out.println(blockingQueue.remove());
         System.out.println(blockingQueue.remove());
//         System.out.println(blockingQueue.remove());
         System.out.println(blockingQueue);
//       第二种情况
//        插入
        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("c");
        System.out.println(blockingQueue.offer("d"));
        System.out.println(blockingQueue);
//        移除
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue);
//第三种情况
        //插入   当插入的数量超出设置值以后  将会出现阻塞
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
//        blockingQueue.put("d");
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());

        System.out.println(blockingQueue);



    }
}
